"""
Copyright (c) 2023 - present SKbioinfo
"""

from apps.NGS_API.connector.db_common import query_info
from apps.NGS_API.charts.utils import define_month_time
from apps.NGS_API.common.db_table_config import *
from flask_restful import reqparse,Resource
from flask import request 
from flask_login import login_required

from apps.NGS_API.utils.log_helper import lg
from apps.NGS_API.charts.common_query import *
import datetime
import re

months = {
        1:"一月",
        2:"二月",
        3:"三月",
        4:"四月",
        5:"五月",
        6:"六月",
        7:"七月",
        8:"八月",
        9:"九月",
        10:"十月",
        11:"十一月",
        12:"十二月",
        0:"十二月",
        -1:"十一月"
    }

'''

'''
class interfaceSampleBar(Resource):
    @login_required
    def get(self):
        try:
            sample_storage = {}
            today = datetime.datetime.today()
            last_month = today.month
            mid_month = last_month - 1
            first_month = mid_month - 1
            three_months=[months[first_month],months[mid_month],months[last_month]]
            sample_storage["months"] = three_months
            ### SQL: monthly sample instock ### 
            sample_storage["instock"] = [0,0,0]
            ### define month time ###
            month_time = define_month_time()
            ### SQL：three months instock infos query ###
            three_months_instock_sql = three_months_interval_query(
                    month_time = month_time,
                    name = "sam",
                    table_name = '''( SELECT * FROM {tablename} WHERE action_type=1 )'''.format(
                            tablename = sample_log["name"]
                    ),
                    col = "create_time"
            )
            ### SQL: instock results ###
            instock_results = query_info(three_months_instock_sql)
            tuple_checks = check_tuple_values(instock_results)
            if tuple_checks:
                instock = [ int(count) for row in instock_results for count in row ]
                instock.reverse()
                sample_storage["instock"] = instock
            else:
                pass
            ### SQL: monthly sample outstock ### 
            sample_storage["outstock"] = [0,0,0]
            three_months_outstock_query = three_months_interval_query(
                    month_time=month_time,
                    name = "sam",
                    table_name = '''( SELECT * FROM {tablename} WHERE action_type=-1 )'''.format(
                            tablename = sample_log["name"]
                    ),
                    col = "create_time"
            )
            outstock_results = query_info(three_months_outstock_query)
            tuple_checks = check_tuple_values(outstock_results)
            if tuple_checks:
                outstock = [ int(count) for row in outstock_results for count in row ]
                outstock.reverse()
                sample_storage["outstock"] = outstock
            else:
                pass
            return sample_storage
        except Exception as e:
            lg.error(e)
            
'''
    INTERFACE: monthly report of samples
    DATA_URL: /api/charts/lineCharts/sample
    TEMPLATE: /template/home/laboratory/NGS/NGS_Samples_search.html
    JAVASCRIPT: /static/interface/interfaceSampleAmount.js
'''
class interfaceMonthlySampleLine(Resource):
    @login_required
    def get(self):
        try:
            recent_samples = {}
            recent_samples["time_point"] = ["0st-6th","6th-12th","12th-18th","18th-24th","24th-30th"]
            recent_sample_number_sql = sum_up_table_column(
                col="sample_id",
                table_name=sample_db["name"]
            )
            recent_sample_number = query_info(recent_sample_number_sql)
            if recent_sample_number is None:
                recent_samples["recent_sample_number"] = 0
            else:
                recent_samples["recent_sample_number"] = recent_sample_number[0][0]
            ### SQL: monthly sample instock statistic ###
            instock = [0,0,0,0,0,0]
            ### define month time ###
            month_time = define_month_time()
            print(month_time)
            ### SQL: monthly instock sample query ###
            monthly_instock_sample_sql = six_days_interval_query(
                month_time = month_time,
                name = "sam",
                table_name = '''( SELECT * FROM {tablename} WHERE action_type=1 )'''.format(
                            tablename = sample_log["name"]
                ),
                col = "create_time"
            )
            monthly_instock_sample = query_info(monthly_instock_sample_sql)
            ### SQL: month instock sample ###
            tup_instock_checks = check_tuple_values(monthly_instock_sample)
            if tup_instock_checks:
                instock = [ int(count) for sample in monthly_instock_sample for count in sample ]
                instock.insert(0,0)
                instock.reverse()
            else:
                pass
            recent_samples["instock"] = instock
            ### SQL: monthly outstock sample query ###
            outstock = [0,0,0,0,0,0]
            monthly_outstock_sample_sql = six_days_interval_query(
                month_time=month_time,
                name = "sam",
                table_name = '''( SELECT * FROM {tablename} WHERE action_type=-1 )'''.format(
                            tablename = sample_log["name"]
                ),
                col = "create_time"
            )
            monthly_outstock_sample = query_info(monthly_outstock_sample_sql)
            tuple_outstock_checks = check_tuple_values(monthly_outstock_sample)
            if tuple_outstock_checks:
                outstock = [ int(count) for sample in monthly_outstock_sample for count in sample ]
                outstock.insert(0,0)
                outstock.reverse()
            else:
                pass
            recent_samples["outstock"] = outstock
            ### SQL: monthly delete sample query ###
            delete = [0,0,0,0,0,0]
            monthly_delete_sample_sql = six_days_interval_query(
                month_time=month_time,
                name = "sam",
                table_name = '''( SELECT * FROM {tablename} WHERE action_type=0 )'''.format(
                            tablename = sample_log["name"]
                ),
                col = "create_time"
            )
            monthly_delete_sample = query_info(monthly_delete_sample_sql)
            tuple_delete_checks = check_tuple_values(monthly_delete_sample)
            if tuple_delete_checks:
                delete = [ int(count) for sample in monthly_delete_sample for count in sample ]
                delete.insert(0,0)
                delete.reverse()
            else:
                pass
            recent_samples["delete"] = delete
            return recent_samples
        except Exception as e:
            lg.error(e)

